package com.yunhe.policyfulfillment.policyful.controller;

import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.model.ResultObject;
import com.yunhe.common.model.mybatis.PageResult;
import com.yunhe.policyfulfillment.authority.domain.PolicyOrg;
import com.yunhe.policyfulfillment.authority.service.IPolicyOrgService;
import com.yunhe.policyfulfillment.constant.PolicyFulfillmentConstant;
import com.yunhe.policyfulfillment.policyful.domain.*;
import com.yunhe.policyfulfillment.policyful.model.*;
import com.yunhe.policyfulfillment.policyful.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

/**
 * @ClassName PortalController
 * @Description TODO
 * @Author HeTao
 * @Date 2021/7/26 9:51
 * @Version 1.0
 **/
@RestController
@RequestMapping(value = "/portal")
@Api(value = "Portal", description = "门户接口", tags = {"Model_Portal"})
public class PortalController {

    @Autowired
    private IPolicyOrgService policyOrgService;

    @Autowired
    private PolicyInfoNewsService policyInfoNewsService;

    @Autowired
    private BannerService bannerService;

    @Autowired
    private IPolicyInfoMatterService policyInfoMatterService;

    @Autowired
    private PolicyInfoService policyInfoService;

    @Autowired
    private PolicyInfoApplyMatterService policyInfoApplyMatterService;

    @Autowired
    private AppealService appealService;

    @Autowired
    private PolicyResultService policyResultService;

    /**
     * 查询所有组织
     * @author hetao
     * @date 2021/07/22
     **/
    @ApiOperation("查询所有组织")
    @GetMapping(value = "/findPolicyOrgs")
    public ResponseEntity<ResultObject<List<PolicyOrg>>> findPolicyOrgs(){
        return new ResponseEntity<>(new ResultObject(policyOrgService.findPolicyOrgs()), HttpStatus.OK);
    }

    @ApiOperation("查询5条热门新闻")
    @GetMapping(value = "/findHotNew")
    public ResponseEntity<ResultObject<List<PolicyInfoNews>>>  findHotNew(){
        return new ResponseEntity<>(new ResultObject(policyInfoNewsService.findHotNew()), HttpStatus.OK);
    }

    @ApiOperation("查询通知公告")
    @GetMapping(value = "/findNewByTitle")
    public ResponseEntity<ResultObject<List<PolicyInfoNews>>>  findNewByTitle(@RequestParam(value = "title",required = false) String title){
        return new ResponseEntity<>(new ResultObject(policyInfoNewsService.findNewByTitle(title)), HttpStatus.OK);
    }

    @ApiOperation("查询通知公告分页")
    @PostMapping(value = "/findNewByCondition")
    public ResponseEntity<ResultObject<List<PolicyInfoNews>>>  findNewByCondition(@RequestBody PortalNewQueryCondition portalNewQueryCondition){
        return new ResponseEntity<>(new ResultObject(policyInfoNewsService.findNewByCondition(portalNewQueryCondition)), HttpStatus.OK);
    }

    @ApiOperation("查询事项分页")
    @PostMapping(value = "/findMatterByCondition")
    public ResponseEntity<ResultObject<List<PolicyInfoMatterVo>>>  findMatterByCondition(@RequestBody PortalMatterQueryCondition portalMatterQueryCondition){
        return new ResponseEntity<>(new ResultObject(policyInfoMatterService.findMatterByCondition(portalMatterQueryCondition)), HttpStatus.OK);
    }

    @ApiOperation("查询事项详情")
    @GetMapping(value = "/findMatterById")
    public ResponseEntity<ResultObject<PolicyInfoMatter>> findMatterById(@RequestParam(value = "id",required = true) long id){
        PolicyInfoMatter policyInfoMatter =  policyInfoMatterService.findPolicyInfoMatterById(id);
        policyInfoMatterService.updateMatterAccessCountById(id);
        return new ResponseEntity<>(new ResultObject(policyInfoMatter), HttpStatus.OK);
    }

    /**
     * 2021-09-17 四项数据暂时按总的统计，不需按年份统计
     * */
    @ApiOperation("事项统计")
    @GetMapping(value = "/getMatterStatistics")
    public ResponseEntity<ResultObject<MatterStatisticsVo>>  getMatterStatistics(){
        return new ResponseEntity<>(new ResultObject(policyInfoMatterService.getMatterStatistics()), HttpStatus.OK);
    }

    @ApiOperation("事项点击")
    @PostMapping(value = "/updateMatterAccessCountById")
    public ResponseEntity<ResultObject<Void>>  updateMatterAccessCountById(@RequestParam(value = "id",required = true) long id){
        policyInfoMatterService.updateMatterAccessCountById(id);
        return new ResponseEntity<>(new ResultObject(), HttpStatus.OK);
    }


    @ApiOperation("查询Banner")
    @GetMapping(value = "/getBanner")
    public ResponseEntity<ResultObject<List<Banner>>>  getBanner(){
        return new ResponseEntity<>(new ResultObject(bannerService.getBanner()), HttpStatus.OK);
    }

    @ApiOperation("根据条件获取政策库分页（门户）")
    @PostMapping(value = "/getPageByConditionForFront")
    public ResponseEntity<ResultObject<List<PolicyInfo>>>  getPageByConditionForFront(@RequestBody PolicyInfoQueryCondition condition){
        //只查询上线的
        condition.setStatus(PolicyFulfillmentConstant.Online_Type.Online.value());
        PageResult result = policyInfoService.selectByConditionForPage(condition);
        return new ResponseEntity<>(new ResultObject(result), HttpStatus.OK);
    }

    @ApiOperation("（首页）获取政策库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "标题", required = false, dataType = "String", paramType = "query")
    })
    @PostMapping(value = "/getPolicyInfoForFrontPage")
    public ResponseEntity<ResultObject<List<PolicyInfo>>>  getPolicyInfoForFrontPage(@RequestParam(value = "title", required = false) String title){
        List<PolicyInfo> result = policyInfoService.selectEightData(title);
        return new ResponseEntity<>(new ResultObject(result), HttpStatus.OK);
    }


    @ApiOperation("（首页）查询结果公示")
    @PostMapping(value = "/getPolicyInfoApplyMatterByCondition")
    public ResponseEntity<ResultObject<List<PortalApplyMatterVo>>>  getPolicyInfoApplyMatterByCondition(@RequestBody PortalApplyMatterQueryCondition portalApplyMatterQueryCondition){
        PageResult result = policyInfoApplyMatterService.getPolicyInfoApplyMatterByCondition(portalApplyMatterQueryCondition);
        return new ResponseEntity<>(new ResultObject(result), HttpStatus.OK);
    }

    @ApiOperation("（首页）查询审批公示")
    @PostMapping(value = "/getPolicyResultByCondition")
    public ResponseEntity<ResultObject<List<PolicyResultVo>>>  getPolicyResultByCondition(@RequestBody PolicyResultQueryCondition policyResultQueryCondition){
        PageResult result = policyResultService.getPolicyResultByCondition(policyResultQueryCondition);
        return new ResponseEntity<>(new ResultObject(result), HttpStatus.OK);
    }

    @ApiOperation("获取诉求统计量")
    @GetMapping(value = "/getAppealStatistic")
    public ResponseEntity<ResultObject<AppealStatistic>> getAppealStatistic() throws ParseException {
        AppealStatistic result = appealService.getAppealStatistic();
        return new ResponseEntity<>(new ResultObject(result), HttpStatus.OK);
    }

    /**
     * 查询 根据主键 id 查询
     * @date 2021/07/23
     **/
    @ApiOperation("查询 新闻")
    @GetMapping("/findPolicyInfoNewsById")
    public ResponseEntity<ResultObject<PolicyInfoNews>> findPolicyInfoNewsById(@RequestParam(value = "id",required = true) long id){
        return new ResponseEntity<>(new ResultObject(policyInfoNewsService.findPolicyInfoNewsById(id)),HttpStatus.OK);
    }

    @ApiOperation("根据条件查询诉求（门户）")
    @PostMapping("/getAppealByConditionPortal")
    public ResponseEntity<ResultObject<List<Appeal>>> getAppealByConditionPortal(@RequestBody AppealCondition appealCondition) throws ParseException {
        PageResult result = appealService.getAppealByConditionPortal(appealCondition);
        return new ResponseEntity<>(new ResultObject(result),HttpStatus.OK);
    }

    @ApiOperation("根据组织类型查询组织")
    @GetMapping(value = "/findPolicyOrgsByOrgType")
    public ResponseEntity<ResultObject<List<PolicyOrg>>> findPolicyOrgsByOrgType(@RequestParam(value = "orgType",required = true) String orgType){
        return new ResponseEntity<>(new ResultObject(policyOrgService.findPolicyOrgsByOrgType(orgType)), HttpStatus.OK);
    }

    @ApiOperation("首页统计分析")
    @PostMapping(value = "/getIndexStatistics")
    public ResponseEntity<ResultObject<List<IndexStatistics>>> getIndexStatistics(){
        return new ResponseEntity<>(new ResultObject(policyInfoMatterService.getIndexStatistics()), HttpStatus.OK);
    }

    @ApiOperation("政策事项分页查询")
    @PostMapping("/findPolicyInfoMatterByQueryCondition")
    public ResponseEntity<ResultObject<List<PolicyInfoMatterVo>>> findPolicyInfoMatterByQueryCondition(@RequestBody PolicyInfoMatterQueryCondition policyInfoMatterQueryCondition) throws ArgumentErrorException {
        policyInfoMatterQueryCondition.setIsOnline(PolicyFulfillmentConstant.Online_Type.Online.value());
        PageResult pageResult = policyInfoMatterService.findPolicyInfoMatterByQueryConditionForPortal(policyInfoMatterQueryCondition);
        return new ResponseEntity<>(new ResultObject(pageResult), HttpStatus.OK);
    }
}
